package com.kanshu.ksgb.fastread.doudou.module.book.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ActivityMgr;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wcy on 18/5/27
 */
public class AdBookDetailsOneLayout extends LinearLayout {
    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.change)
    TextView mChange;
    @BindView(R.id.container)
    LinearLayout mContainer;
    SparseArray<View> mSparseArray = new SparseArray<>();

    public AdBookDetailsOneLayout(Context context) {
        super(context);
        init();
    }

    public AdBookDetailsOneLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdBookDetailsOneLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_book_details_type_one_layout, this);
        ButterKnife.bind(view);
    }

    public void refresh(List<BookInfo> list) {
        if (!Utils.isEmptyList(list)) {
            if (list.size() > 3) {
                list = list.subList(0, 3);
            }
            for (int i = 0; i < list.size(); i++) {
                View view = mSparseArray.get(i);
                if (view == null) {
                    view = LayoutInflater.from(getContext()).inflate(R.layout.item_book_layout, null);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    mContainer.addView(view, params);
                    mSparseArray.put(i, view);
                }
                ImageView imageView = (ImageView) view.findViewById(R.id.cover);
                TextView tv = (TextView) view.findViewById(R.id.book_title);
                TextView tvDesc = (TextView) view.findViewById(R.id.book_desc);
                TextView tvAuthor = (TextView) view.findViewById(R.id.author);
                TextView tvLable = (TextView) view.findViewById(R.id.book_label);
                SuperTextView tvStatus = (SuperTextView) view.findViewById(R.id.book_status);
                View bottomContainer = view.findViewById(R.id.bottom_container);
                DisplayUtils.visible(bottomContainer);
                BookUtils.setStatusBg(tvStatus, list.get(i));
                GlideImageLoader.load(list.get(i).cover_url, imageView);
                tv.setText(list.get(i).book_title);
                tvDesc.setText(list.get(i).book_intro);
                tvAuthor.setText(list.get(i).author_name);
                tvLable.setText(BookUtils.getBookLabel(list.get(i)));
                tvStatus.setText(BookUtils.getBookStatus(list.get(i)));
                final BookInfo item = list.get(i);
                view.setOnClickListener(v -> {
                    ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.book_id);
                    if(ActivityMgr.getInstance().getActivityCount(getContext().getClass()) > 1){
                        if(getContext() instanceof Activity){
                            ((Activity)getContext()).finish();
                        }
                    }
                });
            }
        }
    }
}
